#include "interdialog.h"
#include "ui_interdialog.h"



#include <QDebug>
#include <QMessageBox>


QStringList line_x,line_y;

InterDialog::InterDialog(QWidget *parent) :QDialog(parent),ui(new Ui::InterDialog)
{
    ui->setupUi(this);
    setWindowTitle("拟合算法仿真测试");
    Qt::WindowFlags windowFlag  = Qt::Dialog;
    // windowFlag                  |= Qt::WindowMinimizeButtonHint;
    windowFlag                  |= Qt::WindowMaximizeButtonHint;
    windowFlag                  |= Qt::WindowCloseButtonHint;
    setWindowFlags(windowFlag);

    buildChart();
}

InterDialog::~InterDialog()
{
    delete ui;
}

void InterDialog::buildChart()
{
    QLinearGradient gradient(0, 0, 0, 400);
    gradient.setColorAt(0, QColor(90, 90, 90));
    gradient.setColorAt(0.38, QColor(105, 105, 105));
    gradient.setColorAt(1, QColor(70, 70, 70));
    ui->widget->setBackground(QBrush(gradient));
    ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes |
                                    QCP::iSelectLegend | QCP::iSelectPlottables);
    ui->widget->axisRect()->setupFullAxesBox();
    //X轴范围 标题 颜色 字体设置
    ui->widget->xAxis->setRange(0, 60);
    ui->widget->xAxis->setLabel("X轴位置");
    ui->widget->xAxis->setBasePen(QPen(Qt::white));
    ui->widget->xAxis->setTickPen(QPen(Qt::white));
    ui->widget->xAxis->grid()->setVisible(true);
    ui->widget->xAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));
    ui->widget->xAxis->setTickLabelColor(Qt::white);
    ui->widget->xAxis->setLabelColor(Qt::white);

    //Y轴范围 标题 颜色 字体设置
    ui->widget->yAxis->setRange(-2000, 2000);
    ui->widget->yAxis->setLabel("Y轴值");
    ui->widget->yAxis->setBasePen(QPen(Qt::white));
    ui->widget->yAxis->setTickPen(QPen(Qt::white));
    ui->widget->yAxis->setSubTickPen(QPen(Qt::white));
    ui->widget->yAxis->grid()->setSubGridVisible(true);
    ui->widget->yAxis->setTickLabelColor(Qt::white);
    ui->widget->yAxis->setLabelColor(Qt::white);
    ui->widget->yAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::SolidLine));
    ui->widget->yAxis->grid()->setSubGridPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));

    ui->widget->legend->setVisible(true);
    QFont legendFont = font();   //设置字体
    legendFont.setPointSize(10);
    ui->widget->legend->setFont(legendFont);
    ui->widget->legend->setSelectedFont(legendFont);
    ui->widget->legend->setSelectableParts(QCPLegend::spItems);

    DataGraph = ui->widget->addGraph();
    QPen DataPen;
    DataPen.setColor(QColor(rand()%245+10, rand()%245+10, rand()%245+10));
    DataPen.setWidthF(5);
    DataGraph->setPen(DataPen);
    DataGraph->setLineStyle(QCPGraph::lsNone);
    DataGraph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));

    lglrGraph = ui->widget->addGraph();
    QPen lglrPen;
    lglrPen.setColor(QColor(rand()%245+10, rand()%245+10, rand()%245+10));
    lglrPen.setWidthF(3);
    lglrGraph->setPen(lglrPen);
    lglrGraph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 3));

}

// 导入数据按钮
void InterDialog::on_pushButton_AddData_clicked()
{
    QString path = QFileDialog::getOpenFileName(this,"打开文件","C:\\Users\\Administrator\\Desktop");
    if(path.isEmpty())
    {
        return;
    }
    QFile file(path);  //参数就是读取文件的路径
    //设置打开方式 只读方式
    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QString str;
        QStringList lst;
        QTextStream in(&file);  //用文件构造流
        line_x.clear();
        line_y.clear();
        while (!in.atEnd())
        {
           str = in.readLine();//循环读取下行
           if(str == "")
           {
               goto loop;
           }
           else
           {
               lst.clear();
               lst = str.split('\t');
               line_x.append(lst.at(0));
               line_y.append(lst.at(1));
           }
        }
    }
      loop:
             file.close();

        lglrGraph->data().data()->clear();
        DataGraph->data().data()->clear();
        ui->widget->replot();
        QPen graphPen;
        graphPen.setColor(QColor(rand()%245+10, rand()%245+10, rand()%245+10));
        graphPen.setWidthF(5);
        DataGraph->setPen(graphPen);
        DataGraph->setLineStyle(QCPGraph::lsNone);
        DataGraph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));

        //设定X Y范围
        QString minValue_x = *std::min_element(line_x.begin()+1, line_x.end()-1);
        QString maxValue_x = *std::max_element(line_x.begin()+1, line_x.end()-1);
        QString maxValue_y = *std::max_element(line_y.begin()+1, line_y.end()-1);
        QString minValue_y = *std::min_element(line_y.begin()+1, line_y.end()-1);
        ui->widget->xAxis->setRange(minValue_x.toFloat()-5, maxValue_x.toFloat()+5);
        ui->widget->yAxis->setRange(minValue_y.toFloat()-(qAbs(minValue_y.toFloat())/5 + 5), maxValue_y.toFloat()+(qAbs(maxValue_y.toFloat())/5 + 1));
        for(int i=1;i<line_x.size();i++)
        {
           DataGraph->addData((line_x.at(i)).toFloat(), (line_y.at(i)).toFloat());
           ui->widget->replot();
        }
}

//预先定义插值节点的个数为1000个，根据控制台输入的个数num从而确定插值节点的个数
const int N=1000;
// 拉格朗日插值算法
double InterDialog::lglr(double x[], double y[],int n,int t)
{
     double yResult=0.0;
     //LValue[N]存放的是每次求解的插值基函数的通项
     double LValue[N];
     //循环变量k,m
     int k,m;
     //插值基函数中的上下累乘temp1,temp2
     double temp1,temp2;
     for(k=0;k<n;k++)
     {
         temp1=1.0;
         temp2=1.0;
         for(m=0;m<n;m++)
         {
             if(m==k)
             {
                 continue;
             }
             temp1 *= (t-x[m]);
             temp2 *= (x[k]-x[m]);
         }
         LValue[k]=temp1/temp2;
     }
     for(int i=0;i<n;i++)
     {
         yResult += y[i]*LValue[i];
     }
     return yResult;
}

//
void InterDialog::on_pushButton_lglr_clicked()
{
   if(DataGraph->data().data()->isEmpty())
   {
     QMessageBox::information(this,"提示","原始数据未加载，或者已经被清除");
     return;
   }
    double x[line_x.size()],y[line_y.size()],pre_value[line_x.size()];
    for(int i=0;i<line_x.size();i++)
    {
        x[i] = (line_x.at(i)).toFloat();
        y[i] = (line_y.at(i)).toFloat();
    }

    QPen graphPen;
    graphPen.setColor(QColor(rand()%245+10, rand()%245+10, rand()%245+10));
    graphPen.setWidthF(3);
    lglrGraph->setPen(graphPen);
    lglrGraph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 3));
    lglrGraph->data().data()->clear();
    ui->widget->replot();

    for(int i=1;i<line_x.size();i++)
    {
        pre_value[i-1] = lglr(x,y,line_x.size(),i);
        lglrGraph->addData(i, pre_value[i-1]);
        ui->widget->replot();
    }
}

// 清除曲线
void InterDialog::on_pushButton_clear_clicked()
{
    line_x.clear();
    line_y.clear();
    lglrGraph->data().data()->clear();
    DataGraph->data().data()->clear();
    ui->widget->replot();
}
